home *** CD-ROM | disk | FTP | other *** search
- Path: news.spies.com!usenet
- From: Erik Max Francis <max@alcyone.com>
- Newsgroups: comp.lang.c
- Subject: Re: Help: 2 short functions
- Date: Tue, 02 Apr 1996 08:08:13 -0800
- Organization: Alcyone Systems
- Message-ID: <316150ED.1D0410A3@alcyone.com>
- References: <4ji734$n6v@masala.cc.uh.edu>
- NNTP-Posting-Host: newton.alcyone.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i486)
-
- odie garfield wrote:
-
- > Hi, I have two functions that I'm not too sure about.
- > The first one:
- >
- > char *get_filename(void)
- > {
- > char string[20],*stringp;
- > scanf("%s",string);
- > stringp=&string[0];
- > return stringp;
- > }
- > I'm want the function to read in a string and return a pointer to that string.
- > Am I doing it correctly?
-
- No. You are returning a pointer to an automatic (i.e., local) variable from a
- function. This is a very bad thing. The reason is because automatic variables
- no longer reliably exist outside of the scope of their block. So you're
- returning a pointer to an area which may be reclaimed at any time. Needless to
- say, this is a very bad thing.
-
- It's especially bad because oftentimes, particularly if you're using the pointer
- right after you return from the function, it _seems_ to work. It's just that
- every once in a while (especially on, say, a heavily-loaded multitasking system)
- you'll find that it crashes or causes strange behavior.
-
- Your solutions are either to make the local array static (so it persists between
- calls), or to allocate memory for the array with malloc inside the function and
- return a pointer to that (but be sure to free it later on).
-
- > My second function is related to the first function:
- >
- > FILE *open_file(char *filenamep)
- > {
- > FILE *fp;
- > fp=fopen("??????","r");
- > return fp;
- > }
- > In this 2nd function, I want to open a file using the pointer to a string I
- > got from the first function. But I don't know exactly how to do this. Any help
- > would be appreciated. Thanks.
-
- This is fine. The reason _this_ is fine is because fopen is filling out the
- FILE record that fp is pointing to for you (it actually calls malloc itself;
- fclose appropriately calls free).
-
- Besides, if you look at it strictly, these are two different cases. In the
- first function, you are declaring an automatic variable (char string[20]) and
- returning a pointer to it (char *stringp = string; return stringp). In the
- second, you declaring an automatic variable (FILE *fp) and returning its value
- (return fp). These are two different situation. The first will get you into
- big trouble; the second will not.
-
- --
- Erik Max Francis &tSftDotIotE && http://www.alcyone.com/max && max@alcyone.com
- San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
- H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
- Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
- "Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
-